package news.yp.newsproject.view.fragment;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.news.yp.baselibrary.annotation.ViewById;
import com.news.yp.baselibrary.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import news.yp.newsproject.adapter.BookRecyleAdapter;
import news.yp.newsproject.bean.BookResponse;
import news.yp.newsproject.listener.OnItemClickListener;
import news.yp.newsproject.presenter.presenter.DiscorPresenter;

import news.yp.newsproject.R;
import news.yp.newsproject.bean.ResponsePageData;
import news.yp.newsproject.presenter.presenter.CenterPresenter;
import news.yp.newsproject.view.activity.ShouBookActivity;

/**
 * 显示书籍的fragment
 * 请求路径：118.24.144.127:5000/getbooks
 * 请求参数：String : books  int: page int: num
 * 响应参数：{"messge": "success", "code": 0, "data": [{"bookpath":"aaa","_id":"aaaa","bookjpg":"aaaa"},{"bookpath":"aaa","_id":"aaaa","bookjpg":"aaaa"}]}
 */
public class DiscoverFragment extends BaseFragment<DiscorPresenter> implements CenterPresenter.ViewPresenter<BookResponse> {
    @ViewById(R.id.book_recyview)
    RecyclerView mRecyleview;
    @ViewById(R.id.swiperefrsh)
    SwipeRefreshLayout refreshLayout;

    private List<BookResponse.DataBean> bookLists;
    private BookRecyleAdapter bookAdapter;
    private int page = 0;
    private int num = 20;


    @Override
    public void onLoading() {
        // Toast.makeText(getContext(), "发现页面数据正在加载", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onError() {
        refreshLayout.setEnabled(false);
        Toast.makeText(getContext(), "亲，已无更多数据", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onSuccess(BookResponse responseSectionData) {
        Log.e("TAG", "---" + responseSectionData.toString());
        Log.e("TAG", "---" + responseSectionData.getData().size());
        //清理一下，如果 bookpath为空，那么干掉这条错误数据
        refreshLayout.setRefreshing(false);
        for (int i = 0; i < responseSectionData.getData().size(); i++) {
            if (responseSectionData.getData().get(i).getBookpath() == null) {
                responseSectionData.getData().remove(i);
            }
        }
        bookLists.clear();
        bookLists.addAll(responseSectionData.getData());
        //  Collections.reverse(bookLists);
        Log.e("TAG", "---" + bookLists.size());
        //    bookAdapter.refrsh(bookLists);
        bookAdapter.refrsh(responseSectionData.getData());
    }

    @Override
    public void onPageSuccess(ResponsePageData requestPageData) {

    }

    @Override
    protected void onFragmentVisibleChange(boolean b) {
    }

    @Override
    protected DiscorPresenter createPresenter() {
        return new DiscorPresenter();
    }

    @Override
    protected View setView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.discover_layout, container, false);
    }

    @Override
    protected void initData() {
        getPresenter().getPageData(page, num);
    }

    @Override
    protected void initView() {
        StaggeredGridLayoutManager ssglayout = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        mRecyleview.setLayoutManager(ssglayout);
        mRecyleview.setItemAnimator(new DefaultItemAnimator());
        //mRecyleview.addItemDecoration(new GridSpacingItemDecoration);
        bookLists = new ArrayList<>();
        bookAdapter = new BookRecyleAdapter(getActivity(), bookLists);
        mRecyleview.setAdapter(bookAdapter);
        refreshLayout.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light,
                android.R.color.holo_orange_light, android.R.color.holo_green_light);
        initListener();
        initRefrsh();
        initData();
    }

    /**
     * 初始化监听器
     */
    private void initListener() {
        bookAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemOnClick(View v, int position) {
                //   Toast.makeText(getContext(), "你点击的是"+position, Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getActivity(), ShouBookActivity.class);
                intent.putExtra("bookpath", bookLists.get(position).getBookpath());
                startActivity(intent);
            }
        });
    }

    private void initRefrsh() {
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                // 设置可见
                refreshLayout.setRefreshing(true);
                //开始获取数据
                getPresenter().getPageData(++page, num);
            }
        });
    }
}
